package com.wangzhixuan.apiController;



import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wangzhixuan.commons.base.ApiBaseController;
import com.wangzhixuan.commons.constant.ApiCode;
import com.wangzhixuan.commons.jiguang.PushUtils;
import com.wangzhixuan.commons.scan.MyException;
import com.wangzhixuan.model.Notice;
import com.wangzhixuan.service.IBaseInfoService;
import com.wangzhixuan.service.INoticeService;
import com.wangzhixuan.service.IUserService;


/**
 * @description：公告
 * @author：zhixuan.wang
 * @date：2015/10/1 14:51
 */
//@CrossOrigin(origins = "*")当前版本1.7 会报错，原因版本太低 http://spring.io/guides/gs/rest-service-cors/
@Controller
@RequestMapping("/api")
public class NoiceApiController extends ApiBaseController {
	
    @Autowired
    private IUserService userService;
    
    @Autowired 
    private IBaseInfoService baseInfoService;
    
    @Autowired 
    private INoticeService noticeService;
    
	
	@RequestMapping("/notice/release")
    @ResponseBody
    public Object updateVsersion(Integer userId,String releContent) throws MyException {
		if(userId==null){
			throw new MyException(ApiCode.CODE_500, "请重新登录");
		}
		if(releContent==null){
			throw new MyException(ApiCode.CODE_500, "请填写发布信息");
		}
		Notice notice = new Notice();
		notice.setCreateTime(new Date());
		notice.setNoticeMsg(releContent);
		notice.setUserId(userId);
		boolean b = noticeService.insert(notice);
		if(b){
			List<String> allPhone = userService.selectAllPhone();
			for(String phone : allPhone){
				PushUtils.sendPush(phone,"公告内容："+releContent,"0");					//推送结束
			}
			return renderSuccess(ApiCode.CODE_200, "成功");
		}else{
			return renderError(ApiCode.CODE_500, "发布失败");
		}
        
    }
	
	@RequestMapping("/notice/list")
    @ResponseBody
    public Object groupList(String requestType) throws MyException {
		if(requestType==null){
			throw new MyException(ApiCode.CODE_500, "requestType不能为空");
		}
		List<Map<String,Object>> list = noticeService.selectNotice(requestType);
        return renderSuccess(ApiCode.CODE_200, "成功",list);
    }
}
